package com.invengo.testrfid.app.proxy;

import com.invengo.testrfid.app.entity.ReadTagEntity;
import com.invengo.testrfid.app.proxy.real.RealReadTagInven2910;
import com.invengo.testrfid.app.proxy.subject.SubjectReadTag;

/**
 *
 */
public class ProxyReadTag extends SubjectReadTag {

    public static ProxyReadTag proxyReadTag;
    public static SubjectReadTag subjectReadTag;

    public ProxyReadTag() {
    }

    public static ProxyReadTag getInstance() {

        if (proxyReadTag == null) {
            proxyReadTag = new ProxyReadTag();
            subjectReadTag = RealReadTagInven2910.getInstance();
        }
        return proxyReadTag;
    }

    @Override
    public void connectReader(ConnectReaderStatue connectReaderStatue) {

        subjectReadTag.connectReader(connectReaderStatue);
    }

    @Override
    public void readTagTid(ReadTagCallback readTagCallback) {

        subjectReadTag.readTagTid(readTagCallback);
    }

    @Override
    public void stopRead(StopReadCallback stopReadCallback) {

        subjectReadTag.stopRead(stopReadCallback);
    }

    @Override
    public void disConnectReader(DisConnectReaderStatue disConnectReaderStatue) {

    }

    @Override
    public void writeDataToEpc(String data, WriteTagEpcCallback writeTagEpcCallback) {
        subjectReadTag.writeDataToEpc(data,writeTagEpcCallback);
    }

    @Override
    public void writeDataToEpc(byte[] data, WriteTagEpcCallback writeTagEpcCallback) {
        subjectReadTag.writeDataToEpc(data,writeTagEpcCallback);
    }

    @Override
    public void writeDataToEpc(byte[] data, byte[] epcData, WriteTagEpcCallback writeTagEpcCallback) {
        subjectReadTag.writeDataToEpc(data,epcData,writeTagEpcCallback);
    }

    @Override
    public void readTagAllData(ReadTagAllDataCallbac readTagAllDataCallbac) {
        subjectReadTag.readTagAllData(readTagAllDataCallbac);
    }

    private boolean isSendBack = false;

    /**
     * 标签单次读取
     *
     * @param readTagCallback
     * @param stopReadCallback
     */
    public void singleReadTagTid(final ReadTagAllDataCallbac readTagCallback, final StopReadCallback stopReadCallback) {

        isSendBack = false;
        subjectReadTag.readTagAllData(new ReadTagAllDataCallbac() {
            @Override
            public void onReadTagSuccess(ReadTagEntity readTagEntity) {

                if(!isSendBack) {
                    readTagCallback.onReadTagSuccess(readTagEntity);
                    isSendBack = true;
                }
                subjectReadTag.stopRead(stopReadCallback);
            }

            @Override
            public void onReadTagFailed(String msg) {
                readTagCallback.onReadTagFailed(msg);
            }
        });
    }

    /**
     * 标签单次读取
     *
     * @param readTagCallback
     * @param stopReadCallback
     */
    public void singleReadTagAll(final ReadTagCallback readTagCallback, final StopReadCallback stopReadCallback) {

        subjectReadTag.readTagTid(new ReadTagCallback() {

            @Override
            public void onReadSuccess(String tid) {

                readTagCallback.onReadSuccess(tid);
                subjectReadTag.stopRead(stopReadCallback);
            }

            @Override
            public void onFailed(String message) {
                readTagCallback.onFailed(message);
            }
        });

    }

    //////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////

    //////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////


    /**
     * 连接读写器回调
     */
    public interface ConnectReaderStatue {
        void onConnectSuccess();

        void onConnectFailed();
    }

    public interface DisConnectReaderStatue {
        void onDisConnectSuccess();

        void onDisConnectFailed();
    }

    public interface StopReadCallback {

        void onStopSuccess();

        void onStopFailed();
    }

    public interface WriteTagEpcCallback{

        void onWriteSuccess();
        void onWriteFailed();
    }


    public interface ReadTagAllDataCallbac{

        void onReadTagSuccess(ReadTagEntity readTagEntity);
        void onReadTagFailed(String msg);
    }

    /**
     * 读取标签结果回调
     */
    public static class ReadTagCallback {

        public void onReadSuccess(String tid) {
        }

        ;

        public void onReadSuccess(String tid, String epc, String user) {
        }

        ;

        public void onFailed(String message) {
        }

        ;
    }
}
